package com.todo.strategy.online.application.frontend.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.todo.common.common.entity.response.BizResponse;
import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.exception.BizException;
import com.todo.strategy.common.enums.PlanRelationSceneEnum;
import com.todo.strategy.online.application.convert.StrategyConvert;
import com.todo.strategy.online.application.entity.bo.PlanContentBO;
import com.todo.strategy.online.application.entity.bo.PlanRelationBO;
import com.todo.strategy.online.application.frontend.service.PlanContentAggService;
import com.todo.strategy.online.application.service.PlanContentService;
import com.todo.strategy.online.application.service.PlanRelationService;
import com.todo.strategy.online.iface.request.InsertPlanContentParam;
import com.todo.strategy.online.iface.request.QueryPlanContentByIdParam;
import com.todo.strategy.online.iface.request.RemovePlanContentParam;
import com.todo.strategy.online.iface.request.UpdatePlanContentParam;
import com.todo.strategy.online.iface.response.PlanContentInfoVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author zhangwenxiang
 * @since 2024-03-04 00:19:47
 */
@Slf4j
@Service("planContentAggService")
public class PlanContentAggServiceImpl implements PlanContentAggService {

    @Resource
    private PlanContentService planContentService;
    @Resource
    private PlanRelationService planRelationService;

    @Override
    public BizResponse<Boolean> insertPlanContent(InsertPlanContentParam param) {
        if (param == null || !param.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        PlanRelationBO planRelationBO = planRelationService.queryPlanRelationById(param.getPlanRelationId());
        if (planRelationBO == null) {
            log.error("PlanContentAggService#insertPlanContent 新增计划清单关联内容失败，计划清单细则不存在 保存失败 入参：【{}】", JSONObject.toJSONString(param));
            return BizResponse.paramError("操作失败，数据不存在，请刷新后重试");
        }
        //非用户或非用户攻略
        if (!param.get_user().getUserId().equals(planRelationBO.getBusinessId()) || !planRelationBO.getBusinessScene().equals(PlanRelationSceneEnum.USER_STRATEGY)) {
            log.error("PlanContentAggService#insertPlanContent 新增计划清单关联内容失败，计划清单细则不属于当前用户 保存失败 入参：【{}】", JSONObject.toJSONString(param));
            return BizResponse.paramError("操作失败，当前数据无法操作，请刷新后重试");
        }
        PlanContentBO planContentBO = planContentService.queryPlanContentByRelationId(param.get_user().getUserId(), param.getPlanId(), planRelationBO.getRelationId());
        if (planContentBO != null) {
            log.error("PlanContentAggService#insertPlanContent 新增计划清单关联内容失败，计划清单关联内容已经存在 保存失败 入参：【{}】", JSONObject.toJSONString(param));
            return BizResponse.paramError("操作失败，数据不存在，请刷新后重试");
        }
        PlanContentBO request = StrategyConvert.convertToPlanContentBO(param);
        Boolean insertResult = planContentService.insertPlanContent(request);
        return BizResponse.success(insertResult);
    }

    @Override
    public BizResponse<Boolean> updatePlanContent(UpdatePlanContentParam param) {
        if (param == null || !param.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        PlanContentBO planContentBO = planContentService.queryPlanContentById(param.getId());
        if (planContentBO == null) {
            log.error("PlanContentAggService#updatePlanContent 更新计划清单关联内容失败，计划清单关联内容不存在 更新失败 入参：【{}】", JSONObject.toJSONString(param));
            return BizResponse.paramError("操作失败，数据不存在，请刷新后重试");
        }
        if (!param.get_user().getUserId().equals(planContentBO.getUserId()) || !planContentBO.getPlanId().equals(param.getPlanId())) {
            log.error("PlanContentAggService#updatePlanContent 更新计划清单关联内容失败，计划清单关联内容不属于该用户 更新失败 入参：【{}】", JSONObject.toJSONString(param));
            return BizResponse.paramError("操作失败，数据不存在，请刷新后重试");
        }
        PlanContentBO request = StrategyConvert.convertToPlanContentBO(param);
        Boolean updateResult = planContentService.updatePlanContent(request);
        return BizResponse.success(updateResult);
    }

    @Override
    public BizResponse<Boolean> removePlanContent(RemovePlanContentParam param) {
        if (param == null || !param.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        Boolean removeResult = planContentService.removePlanContentById(param.getId());
        return BizResponse.success(removeResult);
    }

    @Override
    public BizResponse<PlanContentInfoVO> queryPlanContentById(QueryPlanContentByIdParam param) {
        if (param == null || !param.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        PlanContentBO planContentBO = planContentService.queryPlanContentByRelationId(param.get_user().getUserId(), param.getPlanId(), param.getPlanRelationId());
        return BizResponse.success(StrategyConvert.convertToPlanContentInfoVO(planContentBO));
    }
}
